﻿using CodeMan.Rabbitmq.Common;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading;
using RabbitMQ.Client;

namespace CodeMan.Rabbitmq.Consumer02.Consumer
{
    public class SmsReceive
    {
        public static void Sender()
        {
            using (var connection = RabbitUtils.GetConnection().CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    //声明并创建一个队列
                    channel.QueueDeclare(RabbitConstant.QUEUE_SMS, true, false, false, null);
                    // 如果不写basicQos(1)，则自动MQ会将所有请求平均发送给所有消费者
                    // basicQos，MQ不再对消费者一次发送多个请求，而是消费者处理完一个消息后(确认后)，在从队列中获取一个新的
                    channel.BasicQos(0, 1, false);//处理完一个取一个
                    var consumer = new EventingBasicConsumer(channel);
                    int count = 1;
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body.ToArray();
                        var message = Encoding.UTF8.GetString(body);
                        Thread.Sleep(60);//休眠60毫秒
                        Console.WriteLine("{0}.SmsSender02【{1}】-发送短信成功：{2}", count++, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message);
                        channel.BasicAck(ea.DeliveryTag, false);
                    };
                    channel.BasicConsume(RabbitConstant.QUEUE_SMS, false, consumer);
                    Console.WriteLine("Press [Enter] to exit");
                    Console.Read();
                }
            }
        }
    }
}